home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue90 / lg@216.39.145.211 < prev    next >
Encoding:
Text File  |  2003-05-31  |  4.5 KB  |  153 lines

  1. #!/usr/bin/env python
  2. """twdt -- Build the TWDT files (TWDT.html and TWDT.txt.gz) for a Linux Gazette
  3. issue (or issues).  Run "twdt --help" for usage.
  4.  
  5. This program is not guaranteed for issues below #85 (lg.twdtCutoff), and
  6. will abort if forced to do an older issue.  There are hand-made files in CVS
  7. for the older issues.  (Before issue #72 the files were named issue##.html and
  8. issue##.tar.gz instead of TWDT.html and TWDT.tar.gz.)
  9. """
  10. import gzip, os, re, sys
  11. sys.path.insert(0, '/home/lg/lib/python')
  12. from optik import OptionParser
  13. import lg
  14. from lg import debug, die
  15.  
  16. try:
  17.     True, False
  18. except NameError:  # Python < 2.3
  19.     True, False = (1==1), (1==0)
  20.  
  21. USAGE = """\
  22. Usage: %prog [--all] [--issue=##] [--stdout]
  23.   Builds the TWDT.html and TWDT.txt.gz files.  If no arguments, use the current
  24.   directory to deduce the issue number, read the source files and write the
  25.   destination files."""
  26.  
  27. HTML_FILE = "TWDT.html"
  28. TEXT_FILE = "TWDT.txt.gz"
  29. LG_ANSWER_DIR = "/home/lg/data/twdt/"
  30. CUT_RX = re.compile( R"<!--startcut[-= ].*?<!--endcut[-= ]*-->\n", 
  31.     re.DOTALL)
  32.  
  33. lg.DEBUG = True
  34.  
  35.  
  36. def catArticleBody(out, sourceFile):
  37.     """Read the article text from 'sourceFile', delete portions surrounded by
  38.        "startcut ... endcut" comments, and write the result to file handle 
  39.        'out'.
  40.     """
  41.     f = open(sourceFile, 'r')
  42.     text = f.read()
  43.     f.close()
  44.     textCooked = CUT_RX.sub("", text)
  45.     out.write(textCooked)
  46.     
  47.  
  48. def makeHTML(out, issue, dir):
  49.     """Write TWDT.html for issue# 'issue' to the open file object 'out',
  50.        reading the article files from directory 'dir'.  If 'dir' is "", use
  51.        the current directory.
  52.     """
  53.     debug("issue# %d" % issue)
  54.     path = os.path.join(dir, "index.html")
  55.     catArticleBody(out, path)
  56.     for art in lg.issues[issue].articles:
  57.         if art.key == "lg_answer":
  58.             path = os.path.join(LG_ANSWER_DIR, "TWDT.lg_answer%d.html" % issue)
  59.         else:
  60.             path = os.path.join(dir, art.key + ".html")
  61.         debug("%-20s from %s" % (art.key, path))
  62.         catArticleBody(out, path)
  63.     out.write("</BODY></HTML>\n")
  64.  
  65.     
  66.  
  67.  
  68. def makeText(dir):
  69.     """Make TWDT.txt.gz from TWDT.html.  
  70.        Exc: AssertionError, if TWDT.html doesn't exist.
  71.     """
  72.     htmlFile = os.path.join(dir, HTML_FILE)
  73.     textFile = os.path.join(dir, TEXT_FILE)
  74.     assert os.path.exists(htmlFile)
  75.     cmd = "links -dump %s | gzip >| %s" % (htmlFile, textFile)
  76.     debug("Running subcommand:  " + cmd)
  77.     os.system(cmd)
  78.  
  79.     # Check for nulls.
  80.     f = gzip.open(textFile)
  81.     text = f.read()
  82.     f.close()
  83.     if '\0' in text:
  84.         print "Warning: NULL character found in %s!" % textFile
  85.  
  86.  
  87. def processIssue(issue, dir, stdout):
  88.     if stdout:
  89.         out = sys.stdout
  90.     else:
  91.         path = os.path.join(dir, HTML_FILE)
  92.         out = file(path, 'w')
  93.     makeHTML(out, issue, dir)
  94.     out.close()
  95.     if not stdout:
  96.         makeText(dir)
  97.     
  98.  
  99. def getIssueDir(issue):
  100.     return "/LG/www/issue%d" % issue
  101.  
  102. def getLG_AnswerPath(issue, dir):
  103.     return "%s/TWDT.lg_answer%d.html" % (issue, dir)
  104.  
  105. #### TOP-LEVEL OPERATIONS
  106. def doAll():
  107.     issues = range(lg.currentIssue, lg.twdtCutoff - 1, -1)
  108.     debug("Issues are %s" % issues)
  109.     for issue in issues:
  110.         debug("=== Issue %d" % issue)
  111.         dir = getIssueDir(issue)
  112.         processIssue(issue, dir, False)
  113.  
  114. def doIssue(issue, stdout):
  115.     print "Issue# %d" % issue
  116.     dir = "/LG/www/issue%d" % issue
  117.     processIssue(issue, dir, stdout)
  118.  
  119. def doDefault(stdout):
  120.     issue = lg.guessIssue()
  121.     processIssue(issue, "", stdout)
  122.  
  123.  
  124. #### MAIN ROUTINE
  125. def main():
  126.     op = OptionParser(USAGE)
  127.     op.add_option("--all", action="store_true", dest="all", default=0, 
  128.         help="all issues from 85 to current, directory /LG/www/issue##")
  129.     op.add_option("--issue", action="store", dest="issue", default=None, 
  130.         type="int", help="do that issue (integer), directory /LG/www/issue##/")
  131.     op.add_option("--stdout", action="store_true", dest="stdout", default=0,
  132.         help="write TWDT.html to standard output; do not write TWDT.tar.gz")
  133.     options, args = op.parse_args()
  134.     if len(args) != 0:
  135.         op.error("too many command-line arguments")
  136.     if   options.all:
  137.         if   options.issue:
  138.             op.error("can't do --all and --issue simultaneously")
  139.         elif options.stdout:
  140.             op.error("can't do --all and --stdout simultaneously")
  141.         doAll()
  142.     elif options.issue:        
  143.         doIssue(options.issue, options.stdout)
  144.     else:
  145.         doDefault(options.stdout)
  146.  
  147.     
  148.  
  149.  
  150. if __name__ == "__main__":  main()
  151.  
  152. # vim: sw=4 ts=4 expandtab
  153.